<?php defined('SYSPATH') or die('No direct script access.');

class Captcha
{
  public static function render()
  {
    $session = Session::instance();
    $md5_hash = md5(rand(0,999)); 
    $captcha_code = strtoupper(substr($md5_hash, 15, 5)); 
    $session->set('captcha_code', $captcha_code);
    
    $width=70;
    $height=25;
    
    $image = imagecreatetruecolor($width, $height);
    $black = imagecolorallocate($image, 0, 0, 0);
    $white = imagecolorallocate($image, 255, 255, 255);
    imagefilledrectangle($image, 0, 0, $width-1, $height-1, $white);
    
          
    imagestring($image, 5, $width/5, $height/5, $captcha_code, $black);
    
          
    $noise_color = imagecolorallocate($image, 100, 120, 180);
    
    for($i=0; $i<($width*$height)/18; $i++ ) 
    {
     imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }
    	      
    // header("Content-Type: image/jpeg"); 
    Request::current()->response()->headers('Content-Type', 'image/jpeg');    
    ImageJpeg($image);
    
    ImageDestroy($image);
  }
}
?>